package com.audi.view;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.audi.R;
import com.audi.adapter.SQLiteAsusNewHelper;
import com.audi.dto.BoxItem;

public class HomeBoxSettingsDialog {

	Activity activity;
	private SQLiteAsusNewHelper mSQLHelper;

	public HomeBoxSettingsDialog(Activity activity) {
		this.activity = activity;
		mSQLHelper=new SQLiteAsusNewHelper(activity);
	}
	

	public void homeboxEditname() {
		 final BoxItem boxItem=new BoxItem() ;
		final Dialog diaEditName;
		diaEditName = new Dialog(activity);
		diaEditName.requestWindowFeature(Window.FEATURE_NO_TITLE);
		diaEditName.setContentView(R.layout.edit_name_homebox_dialog);
		 final EditText newnamebox = (EditText) diaEditName.findViewById(R.id.edit_name_box);
		boxItem.setBoxId(1);
		
		
		
		Button cancel = (Button) diaEditName
				.findViewById(R.id.btn_hb_editname_cancel);
		Button ok = (Button) diaEditName.findViewById(R.id.btn_hb_editname_ok);
		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				diaEditName.dismiss();
			}
		});
		ok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				boxItem.setBoxName(newnamebox.getText().toString());
				mSQLHelper.editNamebox(boxItem);
				Toast.makeText(activity,
						boxItem.getBoxName(), Toast.LENGTH_SHORT).show();
				diaEditName.dismiss();
			}
		});

		diaEditName.show();
	}

	public void homeboxEditpass() {

		final Dialog diaEditpass;
		diaEditpass = new Dialog(activity);
		diaEditpass.requestWindowFeature(Window.FEATURE_NO_TITLE);
		diaEditpass.setContentView(R.layout.edit_password_homebox_dialog);
		// TextView cam = (TextView) diaEditName.findViewById(R.id.text_camera);
		ImageView help = (ImageView) diaEditpass.findViewById(R.id.help_password);
		help.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				final Dialog diaEditpass_rule;
				diaEditpass_rule = new Dialog(activity);
				diaEditpass_rule.requestWindowFeature(Window.FEATURE_NO_TITLE);
				diaEditpass_rule.setContentView(R.layout.passwordrules_dialog);
				diaEditpass_rule.show();
				Button ok = (Button) diaEditpass_rule.findViewById(R.id.ok_btn);
				ok.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						diaEditpass_rule.dismiss();
					}
				});
			}
		});
		diaEditpass.show();
		 
		Button ok = (Button) diaEditpass.findViewById(R.id.btn_ok);
		Button cancle = (Button) diaEditpass.findViewById(R.id.btn_cancel);
		cancle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				diaEditpass.dismiss();
			}
		});
		
		ok.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				 EditText oldpass = (EditText) diaEditpass.findViewById(R.id.edit_old_pass1);
				 EditText newpass = (EditText) diaEditpass.findViewById(R.id.edit_new_pass1);
				 EditText newpassRe = (EditText) diaEditpass.findViewById(R.id.edit_new_pass_re1);
				 final String checkString = "#@$^%&^%*$";
				 if (oldpass.getText().toString().equals("")) {
					Toast.makeText(activity, "Wrong password! Try it again.", Toast.LENGTH_SHORT).show();
					
				}
				else {
					if (newpass.getText().toString().equals(newpassRe.getText().toString())) {
						//String pass=newpass.getText().toString();
						if (newpass.getText().toString()
								.length() >= 8) {
						boolean check = false;
						for (int i = 0; i < checkString
									.length(); i++) {
							if (newpass.getText().toString()
										.indexOf(String
													.valueOf(checkString
																.charAt(i))) >= 0) {
								check = true;
							}
						}
						if (check) {
							Toast.makeText(
										activity,
										"Not char #@$^%&^%*$",
										Toast.LENGTH_SHORT)
										.show();
						} else {
							diaEditpass
										.dismiss();
							
						}
					} else {
						Toast.makeText(
									activity,
									"Password to 8 - 25 ",
									Toast.LENGTH_SHORT)
									.show();
					}
						Toast.makeText(activity, "DONE", Toast.LENGTH_SHORT).show();
						//diaEditpass.dismiss();
						//Toast.makeText(activity, oldpass.getText().toString(), Toast.LENGTH_SHORT).show();
					}else{
						
						Toast.makeText(activity, 
								"Incorrect password. Please confirm your password again.", Toast.LENGTH_SHORT).show();
					
					}
					
				}
				
			}
		});
	}
//	private void showPasswordDialog() {
//		final AudiDialog dialogBoxPassword = new AudiDialog(
//					activity);
//		dialogBoxPassword
//					.setLayout(R.layout.dialog_home_box_password);
//		dialogBoxPassword.show();
//		final String checkString = "#@$^%&^%*$";
//
//		final EditText edtPassword = (EditText) dialogBoxPassword
//					.findViewById(R.id.edt_box_password);
//		Button btnOk = (Button) dialogBoxPassword
//					.findViewById(R.id.btn_ok);
//		
//
//		btnOk.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(View arg0) {
//				if (edtPassword.getText()
//							.toString().length() > 0) {
//					
//				} else {
//					Toast.makeText(
//								activity,
//								"Please input password",
//								Toast.LENGTH_SHORT)
//								.show();
//				}
//			}
//		});
//
//
//	}

}
